package com.example.layui.controller;

import com.example.layui.pojo.Order;
import com.example.layui.pojo.Product;
import com.example.layui.pojo.User;
import com.example.layui.service.OrderService;
import com.example.layui.mapper.ProductDao;
import com.example.layui.service.ProductService;
import com.example.layui.util.ResponseResult;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@RestController
public class OrderController {

    @Resource
    OrderService orderService;
    @Resource
    StringRedisTemplate stringRedisTemplate;



    @RequestMapping("/order/create")
    public ResponseResult orderCreate(int num, int pid){
        //从redis中拿库存
        Integer stock=Integer.valueOf(stringRedisTemplate.opsForValue().get(String.valueOf(pid)));
        if(num>stock){
            return new ResponseResult(0,"库存不足，下单失败","");
        }
        //创建订单
        orderService.createOrder(num, pid);
        return new ResponseResult(0,"下单成功","");
    }

    @RequestMapping("/bangke/order/list")
    public ResponseResult orderList(Order order, Integer page, Integer limit){
        //获取security登陆的用户
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return new ResponseResult(0, ""
                , orderService.count(order,user.getId())
                , orderService.queryByPage(order,user.getId(), (page - 1) * limit, limit));
    }
}
